<template>
  <div>
    <!-- 面包屑导航   -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人信息</el-breadcrumb-item>
      <el-breadcrumb-item>修改个人信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图   -->
    <el-card class="box-card">
      <!--  用户列表区域    -->
      <el-table :data="userList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="性别">
          <template slot-scope="scope">
            {{ scope.row.gender ? '女' : '男' }}
          </template>
        </el-table-column>
        <el-table-column label="电话号码" prop="phone_number"></el-table-column>
        <el-table-column label="身份">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.type" @change="userStateChanged(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="身份证号码" prop="identify_number"></el-table-column>
        <el-table-column label="身份正面照片" prop="identify_positive"></el-table-column>
        <el-table-column label="身份反面照片" prop="identify_reverse"></el-table-column>
        <el-table-column label="openid" prop="openid"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!--     修改按钮       -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
            <!--     删除按钮       -->
<!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>-->
<!--            &lt;!&ndash;     分配角色按钮       &ndash;&gt;-->
<!--            <el-tooltip effect="dark" content="查看详情" placement="top" :enterable="false">-->
<!--              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>-->
<!--            </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加用户的对话框   -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="35%" @close="addDialogClose">
      <!--  内容主体区域    -->
      <el-form :rules="addFormRules" :model="addForm" ref="addFormRef" label-width="90px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone_number">
          <el-input v-model="addForm.phone_number"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone_number">
          <el-select v-model="addForm.gender" placeholder="请选择">
            <el-option v-for="item in genderOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!--  底部区域    -->
      <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!--  修改用户的对话框  -->
    <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <el-form ref="editFormRef" :model="editForm" label-width="100px" :rules="editFormRules">
        <el-form-item label="用户名">
          <el-input v-model="editForm.name" prop="name"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="editForm.gender" placeholder="请选择">
            <el-option v-for="item in genderOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone_number">
          <el-input v-model="editForm.phone_number"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="editForm.identify_number"></el-input>
        </el-form-item>
        <el-form-item label="身份证正面照片">
          <el-input v-model="editForm.identify_positive"></el-input>
        </el-form-item>
        <el-form-item label="身份证反面照片">
          <el-input v-model="editForm.identify_reverse"></el-input>
        </el-form-item>
        <el-form-item label="openid">
          <el-input v-model="editForm.openid"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
         <el-button @click="editDialogVisible = false">取 消</el-button>
         <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^1(3|4|5|6|7|8|9)\d{9}$/

      if (regMobile.test(value)) {
        // 合法的手机号
        return cb()
      }
      // 不合法
      cb(new Error('请输入合法的手机号'))
    }
    return {
      // 获取用户列表的参数
      queryInfo: {
        username: window.sessionStorage.getItem('username'),
        action: 'list'
      },
      userList: [],
      //  控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      //  添加用户的表单数据
      addForm: {
        name: '',
        phone_number: '',
        gender: 0,
        identify_number: '',
        identify_positive: '',
        identify_reverse: '',
        openid: '',
        type: 0
      },
      //  添加表单的验证规则
      addFormRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' } , { min: 2 ,max: 10 ,message: '用户名的长度在3~10之间', trigger: 'blur' }
        ] ,
        phone_number: [
          { required: true, message: '请输入电话号码', trigger: 'blur' } , { validator: checkMobile, trigger: 'blur' }
        ]
      },
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 查询到用户信息对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' } , { min: 2 ,max: 10 ,message: '用户名的长度在3~10之间', trigger: 'blur' }
        ] ,
        phone_number: [
          { required: true, message: '请输入电话号码', trigger: 'blur' } , { validator: checkMobile, trigger: 'blur' }
        ]
      },
      genderOptions: [{
        value: 0,
        label: '男'
      }, {
        value: 1,
        label: '女'
      }]
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      console.log(this.queryInfo)
      const { data: res } = await this.$http.get('/api/mgr/rsUser', { params: this.queryInfo })
      if (res.status !== 1) {
        return this.$message.error('获取用户列表失败')
      }
      console.log(res.data)
      const list = [{
        name: res.data.name,
        gender: res.data.gender,
        identify_number: res.data.identify_number,
        identify_positive: res.data.identify_positive,
        identify_reverse: res.data.identify_number,
        openid: res.data.openid,
        phone_number: res.data.phone_number,
        type: res.data.type,
        id: res.data.id
      }]
      this.userList = list
      console.log(res)
    },
    //  监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getUserList()
    },
    //  监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pageNum = newPage
      this.getUserList()
    },
    // 监听 switch 开关状态的改变
    async userStateChanged(userinfo) {
      console.log('userinfo',userinfo.type)
      const queryInfo = {
        action: 'modify_customer',
        id: userinfo.id,
        newdata: {
          type: userinfo.type
        }
      }
      const { data: res } = await this.$http.put('/api/mgr/customers', queryInfo)
      this.getUserList()
      console.log(res)
    },
    // 监听添加用户对话框的关闭事件
    addDialogClose(){
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮，添加新用户
    addUser() {
      this.$refs.addFormRef.validate(async valid => {
        console.log(valid)
        const queryInfo = {
          action: 'add_customer',
          data: this.addForm
        }
        if (!valid) return
        // 可以发起添加用户的网络请求
        const { data: res } = await this.$http.post('/api/mgr/customers', queryInfo)
        console.log(res)
        if(res.ret !== 0){
          this.$message.error('添加用户失败！')
        }
        this.$message.success('添加用户成功')
        // 隐藏添加用户的对话框
        this.addDialogVisible = false
        // 重新刷新列表
        this.getUserList()
      })
    },
    // 展实编辑用户对话框
    showEditDialog(data) {
      console.log(data)
      this.editForm = data
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交
    editUserInfo(){
      this.$refs.editFormRef.validate(async valid => {
        if(!valid) return
        // 发起修改用户信息的数据请求
        const queryInfo = {
          action: 'modify_customer',
          id: this.editForm.id,
          newdata: this.editForm
        }
        const { data: res } = await this.$http.put('/api/mgr/customers', queryInfo)
        if(res.ret !== 0){
          this.$message.error('添加用户失败！')
        }
        this.$message.success('添加用户成功')
        // 重新刷新列表
        this.getUserList()
        this.editDialogVisible = false
      })
    },
    // 根据ID删除对应的用户
    async removeUserById(id){
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err )

      // 如果用户确认删除，则返回值为字符串confirm
      // 如果用户取消删除，则返回值为字符串cancel
      console.log(confirmResult)
      const queryInfo = {
        action: 'modify_customer',
        id: id,
        newdata: {
          is_delete: 1
        }
      }
      if(confirmResult !== 'confirm'){
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.put('/api/mgr/customers', queryInfo)
      if(res.ret !== 0){
        this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      this.getUserList()
    }
  }
}
</script>

<style scoped>

</style>
